package com.loongtech.core.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间戳计算辅助类
 */
public class Time
{
	public static final long	SEC		= 1000L;	// 秒
	public static final long	MIN		= 60 * SEC;	// 分
	public static final long	HOUR	= 60 * MIN;	// 小时
	public static final long	DAY		= 24 * HOUR;// 天
	public static final long	WEEK = 7 * DAY;

    public static final String TIMEPATTERN_SIMPLE = "yyyy-MM-dd";
	public static final String TIMEPATTERN_ALL = "yyyy-MM-dd HH:mm";
	public static final String TIMEPATTERN_ALL2 = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String TIMEPATTERN_ALL3 = "yyyy-MM-dd HH:mm:ss";
	public static final SimpleDateFormat SDF_ALL = new SimpleDateFormat("YYYY-MM-dd HH:mm");

	public static Date getDateByAddNum(Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, num);
		return cal.getTime();
	}

    public static String getDateByNum(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, num);
        return new SimpleDateFormat("MM-dd").format(cal.getTime());
    }

    public static String getDateByHNum(Date date, int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, num);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }
	
	public static String formatDate(Date date, String pattern) {
		String strDate = null;
		if (date != null) {
			try {
				if (pattern == null) {
					pattern = TIMEPATTERN_SIMPLE;
				}
				SimpleDateFormat format = new SimpleDateFormat(pattern);
				strDate = format.format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return strDate;
	}
	/*public static void main(String[] args)
	{
		long time = System.currentTimeMillis();
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 7 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 1 * DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 2 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 3 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 4 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 5 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time - 6 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time + 1 * DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time + 2 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time + 3 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time +4 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time + 5 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time + 6 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfWeek(time + 7 *DAY),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.getBeginningOfMonth(time),"yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println(Time.formatTime(Time.add(Time.getBeginningOfMonth(time),Calendar.MONTH,-1),"yyyy-MM-dd HH:mm:ss.SSS"));
	}
*/
	public static long currentTime() {
		return System.currentTimeMillis();
	}
	
	public static Date currentDate() {
		return new Date();
	}
	
	/**
	 * 返回日期的yyyy-MM-dd串
	 * @param date
	 * @return
	 */
	public static String getDateStr(Date date) {
		if (date == null) {
			return "";
		}

		SimpleDateFormat format = new SimpleDateFormat(TIMEPATTERN_SIMPLE);
		return format.format(date.getTime());
	}

    public static String getMonthStr(Date date) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date.getTime());
    }

    public static String getHHmm() {
        return getNewDateDetailStr().split(" ")[1];
    }

	public static String getDateAndHourStr(Date date) {
		if (date == null) {
			return "";
		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date.getTime());
	}
	
	/**
	 * 格式化时间戳
	 * @param timestamp
	 * @param pattern
	 * @return
	 */
	public static String formatTime(long timestamp, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(new Date(timestamp));
	}
	
	/**
	 * 得到当前详细时间字符串
	 * @return
	 */
	public static String getNewDateDetailStr() {
		return formatTime(currentTime(), TIMEPATTERN_ALL);
	}
	
	/**
	 * 得到当前详细时间字符串
	 * @return
	 */
	public static String getNowDateDetailStr() {
		return formatTime(currentTime(), TIMEPATTERN_ALL2);
	}
	
	/**
	 * 根据时分秒配置 获取今天配置时间点
	 * @return
	 */
	public static long formatDateStr(String dateStr, String pattern) {
		try {
			if (dateStr == null || "".equals(dateStr)) {
				return 0;
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(pattern);			
			return bartDateFormat.parse(dateStr).getTime();
		} catch (Exception ex) {
			throw new SysException(ex);
		}
	}
	
	/**
	 * 根据时分秒配置 获取对应时间
	 * @return
	 */
	public static java.util.Date formatDate(String dateStr, String pattern) {
		try {
			if (dateStr == null || "".equals(dateStr)) {
				return new Date(0);
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(pattern);	
			return bartDateFormat.parse(dateStr);
		} catch (Exception ex) {
			throw new SysException(ex);
		}
	}

    public static String getCeil1(long time) {
        Calendar ca = Calendar.getInstance();
        if (time > 0L) {
            ca.setTimeInMillis(time);
        }
        ca.set(Calendar.MINUTE, ca.get(Calendar.MINUTE));
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MILLISECOND, 0);
        return formatTime(ca.getTimeInMillis(), TIMEPATTERN_ALL);
    }
	
	/**
	 * 传入时间下取整五分钟后的格式化时间字符串
	 * @param time
	 * @return
	 */
	public static String getCeil5(long time) {
		Calendar ca = Calendar.getInstance();
		if (time > 0L) {
			ca.setTimeInMillis(time);
		}
		ca.set(Calendar.MINUTE, ca.get(Calendar.MINUTE) - ca.get(Calendar.MINUTE) % 5);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return formatTime(ca.getTimeInMillis(), TIMEPATTERN_ALL);
	}
	
	/**
	 * 获取给定时间当前凌晨的时间对象，如果给定的时间不存在则取今天凌晨
	 * @param time 取当天凌晨的话传入-1即可
	 * @return
	 */
	public static long getBeginningOfDay(long time) {
		Calendar ca = Calendar.getInstance();
		if (time > 0L) {
			ca.setTimeInMillis(time);
		}
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTimeInMillis();
	}
	
	public static long getEndOfDay(long time) {
		Calendar ca = Calendar.getInstance();
		if (time > 0L) {
			ca.setTimeInMillis(time);
		}
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		ca.set(Calendar.MILLISECOND, 999);
		return ca.getTimeInMillis();
	}
	
	/**
	 * 获取给定时间所在周的开始时间,按照中国习惯取周一凌晨时间
	 * @param time
	 * @return
	 */
	public static long getBeginningOfWeek(long time)
	{
		Calendar ca = Calendar.getInstance();
		if (time > 0L)
		{
			ca.setTimeInMillis(time - DAY);
		}
		else
		{
			ca.setTimeInMillis(System.currentTimeMillis() - DAY);
		}
		ca.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTimeInMillis() + DAY;
	}
	
	/**
	 * 获取给定时间所在月的开始时间
	 * @param time
	 * @return
	 */
	public static long getBeginningOfMonth(long time)
	{
		Calendar ca = Calendar.getInstance();
		if (time > 0L)
		{
			ca.setTimeInMillis(time);
		}
		else
		{
			ca.setTimeInMillis(System.currentTimeMillis());
		}
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTimeInMillis();
	}
	
	/**
	 * 对指定时间做运算
	 * @param time
	 * @param field Calendar.Field
	 * @param addValue 可以为负数
	 * @return
	 */
	public static long add(long time, int field, int addValue)
	{
		Calendar ca = Calendar.getInstance();
		if (time > 0L)
		{
			ca.setTimeInMillis(time);
		}
		else
		{
			ca.setTimeInMillis(System.currentTimeMillis());
		}
		
		ca.add(field, addValue);
		return ca.getTimeInMillis();
	}
	
	/**
	 * 返回date2比date1多多少天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getInterval(Date date1, Date date2) {
		return (int)((date2.getTime() - date1.getTime()) / DAY);
	}

    /**
     * datae1是否比date2大
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isBig(Date date1, Date date2) {
        return date1.getTime() > date2.getTime();
    }

    /**
     * 转换 HH:mm 为long
     * @param hourMin
     * @return
     */
    public static long convertHourMin2Long(String hourMin) {
        try {
            String[] hm = hourMin.split(":");
            return Integer.parseInt(hm[0]) * Time.HOUR + Integer.parseInt(hm[1]) * Time.MIN;
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public static int getHour(long time) {
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(time);
        return ca.get(Calendar.HOUR_OF_DAY);
    }

    public static boolean isToday(Date date) {
        return Time.getInterval(date, Time.currentDate()) == 0;
    }
}
